package information;
import java.util.LinkedList;
import java.util.List;

import processing.core.PConstants;

import util.Coordinaat;
import util.IllegalCoordinaatException;
import util.IllegalWindRichtingException;
import util.WindRichting;

/**
 * This class represents a pacman. PacmanObservers
 * can attach themselves to this class to get notified
 * when pacmans status changes.
 * 
 * @author	Daan Wendelen
 */
public class Pacman
{
	//TODO doc
	public Pacman(Coordinaat beginPosition)
	{
		setPosition(beginPosition);
	}
	
	/**
	 * Moved the pacman.
	 * 
	 * @throws	IllegalWindRichtingException
	 * 			direction == null
	 */
	public void move(WindRichting direction)
	{
		if(direction == null)
			throw new IllegalWindRichtingException();
		
		setPosition(getPosition().plus(direction.getVector()));
	}
	
	/**
	 * Returns the current position.
	 * 
	 * @return	result != null
	 */
	public Coordinaat getPosition()
	{
		return position;
	}
	
	/**
	 * Sets a the new position.
	 * 
	 * @throws	IllegalCoordinaatException
	 * 			direction == null
	 */
	private void setPosition(Coordinaat newPosition)
	{
		if(newPosition == null)
			throw new IllegalCoordinaatException();
		
		position = newPosition;
		
		for(PacmanObserver observer: observers)
			observer.pacmanMoved(newPosition);
	}
	
	private Coordinaat position;
	
	/**
	 * Attaches a observer to this class. When attaching
	 * a observer twice, it will be called twices as well.
	 * 
	 * @throws	NullPointerException
	 * 			pacmanObserver == null
	 */
	public void attach(PacmanObserver pacmanObserver)
	{
		if(pacmanObserver == null)
			throw new NullPointerException();
		
		observers.add(pacmanObserver);
	}
	
	private List<PacmanObserver> observers = new LinkedList<PacmanObserver>();
}
